<template>
  <van-nav-bar
    title="注册"
    left-text="返回"
    left-arrow
    @click-left="$router.back()"
  />
  <van-field v-model="tel" type="tel" label="手机号" />
  <van-button round type="danger" @click="submit" :disabled="tel.length != 11"
    >下一步</van-button
  >
</template>
    
<script setup lang="ts">
import { ref } from "vue";
import { showConfirmDialog } from "vant";
import { useRouter } from "vue-router";
import { CheckPhone } from "@/api/api";

const router = useRouter();
const tel = ref("");
interface IApiCode {
  code: string;
  data: number;
  message: string;
}

const submit = async () => {
  const check = await CheckPhone({ tel: tel.value });
  console.log(check);
  if ((check as unknown as IApiCode).code === "200") {
    router.push({
      path: "/registtwo",
      query: {
        tel: tel.value,
      },
    });
  } else {
    showConfirmDialog({
      title: "提示",
      message: `手机号${tel.value}已注册，是否登录`,
    }).then(() => {
      router.push({
        path: "/login",
        query: {
          tel: tel.value,
        },
      });
    });
  }
  // .then(() => {
  //   router.push({
  //     path: "/registtwo",
  //     query: {
  //       tel: tel.value,
  //     },
  //   });
  // })
  // .catch(() => {
  //   showConfirmDialog({
  //     title: "提示",
  //     message: `手机号${tel.value}已注册，是否登录`,
  //   });
  // });
};
</script>

<style lang="scss" scoped>
.van-cell {
  margin-top: 4rem;
  margin-bottom: 2rem;
}
</style>